/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel.test
 * @date 08/02/2014 - 12:41:17
 */
public class ImageScrollPanel extends JScrollPane implements JXComponent {
	
	private static final long serialVersionUID = -5965712097648279723L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	private final static String ZOOM_ACTION = "zoomAction";
	
	private final ImagePanel imagePanel;
	
	/**
	 * 
	 */
	public ImageScrollPanel() {
	
		super();
		
		imagePanel = new ImagePanel();
		
		initGUI();
	}
	
	/**
	 * @param bufferedImage
	 */
	public ImageScrollPanel(final BufferedImage bufferedImage) {
	
		super();
		
		imagePanel = new ImagePanel(bufferedImage);
		
		initGUI();
	}
	
	/**
	 * @param imageIcon
	 */
	public ImageScrollPanel(final ImageIcon imageIcon) {
	
		super();
		
		imagePanel = new ImagePanel(imageIcon);
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private final void initGUI() {
	
		//
		setViewportView(getImagePanel());
		setPreferredSize(getImagePanel().getPreferredSize());
		
		//
		getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(ZOOM_ACTION), ZOOM_ACTION);
		getActionMap().put(ZOOM_ACTION, new ZoomAction(getImagePanel()));
		
		//
		addMouseWheelListener(new MouseWheelListener() {
			
			@Override
			public void mouseWheelMoved(final MouseWheelEvent mouseWheelEvent) {
			
				//
				if(mouseWheelEvent.isControlDown()) {
					
					final JComponent component = (JComponent)mouseWheelEvent.getComponent();
					
					final Action action = component.getActionMap().get(ZOOM_ACTION);
					
					//
					if(action != null) {
						
						//
						if(action instanceof ZoomAction) {
							
							((ZoomAction)action).setMouseWheelValue(mouseWheelEvent.getWheelRotation());
							
							action.actionPerformed(null);
						}
					}
				}
			}
		});
	}
	
	/**
	 * @return imageIcon in byte array or null
	 */
	public final byte[] getByteArray() {
	
		return getImagePanel().getByteArray();
	}
	
	/**
	 * @return imageIcon
	 */
	public final ImageIcon getImage() {
	
		return getImagePanel().getImage();
	}
	
	/**
	 * @return the imagePanel
	 */
	public final ImagePanel getImagePanel() {
	
		return imagePanel;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#getIndex()
	 */
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isAtributte()
	 */
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequired()
	 */
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setAtributte(boolean)
	 */
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	/**
	 * @param dataByte
	 */
	public final void setImage(final byte[] dataByte) {
	
		getImagePanel().setImage(dataByte);
	}
	
	/**
	 * @param dataByte
	 * @param adjustingScale
	 */
	public final void setImage(final byte[] dataByte, final boolean adjustingScale) {
		
		getImagePanel().setImage(dataByte, adjustingScale);
	}
	
	/**
	 * @param imageIcon
	 */
	public final void setImage(final ImageIcon imageIcon) {
		
		getImagePanel().setImage(imageIcon);
	}
	
	/**
	 * @param imageIcon
	 * @param adjustingScale
	 */
	public final void setImage(final ImageIcon imageIcon, final boolean adjustingScale) {
		
		getImagePanel().setImage(imageIcon, adjustingScale);
	}
	
	/**
	 * @param imageIcon
	 * @param newScale
	 */
	public final void setImage(final ImageIcon imageIcon, final double newScale) {
		
		getImagePanel().setImage(imageIcon, newScale);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setIndex(int)
	 */
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequired(boolean)
	 */
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Panel.border"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
	
	/**
	 * @author Denilson Edinaldo Pinto
	 * @porject jxSwing
	 * @package org.jxSwing.component.panel
	 * @date 08/02/2014 - 12:36:15
	 */
	public class ZoomAction extends AbstractAction {
		
		private static final long serialVersionUID = -120254430250577929L;
		
		private final ImagePanel imagePanel;
		
		private int mouseWheelValue;
		
		/**
		 * @param imagePanel
		 */
		public ZoomAction(final ImagePanel imagePanel) {
		
			this.imagePanel = imagePanel;
		}
		
		@Override
		public void actionPerformed(final ActionEvent actionEvent) {
		
			//
			if(mouseWheelValue > 0) {
				
				imagePanel.downScale(0.01);
				
			}else {
				
				imagePanel.upScale(0.01);
			}
		}
		
		/**
		 * @param mouseWheelValue
		 */
		public void setMouseWheelValue(final int mouseWheelValue) {
		
			this.mouseWheelValue = mouseWheelValue;
		}
	}
}
